package com.nostra13.example.universalimageloader;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

public class ImageExtractor {

	public static String FROM_REPLACEMENT = "FROM_NUMBER";
	public static String PHOTOS_URI = "?&FROM=" + FROM_REPLACEMENT + "&LNG=RU&NO_ICONS=0&CATEG=-1";
	public static String PREVIEW_IMAGES = "preview_images";
	public static String ORIGINAL_IMAGES = "original_images";
	
	private String url;
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}
	
	public ImageExtractor(String url) {
		// TODO Auto-generated constructor stub
		setUrl(url);
	}
	
	public Map<String, String> getImages() throws IOException {
		Document main = Jsoup.connect(getUrl()).timeout(120000).get();
		Elements previewLinks = main.select("a[href$=#picture]");
		Map<String, String> jpgs = new LinkedHashMap<String, String>();
		for(Element previewLink: previewLinks)
        {
			if(previewLink.children().size() != 0) {
				Element previewJPG = previewLink.child(0);
				String previewJPGURL = previewJPG.attr("abs:src");
				String viewJPGURL = getFastViewImgURL(previewJPGURL);
				jpgs.put(previewJPGURL, viewJPGURL);
				logDebug("images", previewJPGURL + " " + viewJPGURL);
			}
        }
		
		
		return jpgs;
	}
	
	private String getFastViewImgURL(String previewURL) {
		return previewURL.replace("icons/", "").replace("-s.jpg", ".jpg");
	}
	
	private void logDebug(String label, String text) {
		Log.d(label, text);
		//System.out.println(label + ": " + text);
	}
	
	public static void main(String[] args) throws IOException {
		ImageExtractor imageExtractor = new ImageExtractor("http://parovoz.com/newgallery");
		imageExtractor.getImages();
	}
}
